

//
//  WOValidate.m
//  BasicProjectTemplate
//
//  Created by 吴欧 on 15/10/21.
//  Copyright (c) 2015年 吴欧. All rights reserved.
//

#import "WOValidate.h"

@implementation WOValidate

/*邮箱验证*/
+ (BOOL)isValidateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

/*网站格式验证*/
+ (BOOL)isValidateHtml:(NSString *)html
{
    NSString *htmlRegex = @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
    NSPredicate *htmlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", htmlRegex];
    return [htmlTest evaluateWithObject:html];
}

/*姓名验证，姓名为2-6个汉字*/
+ (BOOL)isValidateUserName:(NSString *)name
{
    NSString *nameRegex = @"[\u4E00-\u9FA5]{2,6}(?:·[\u4E00-\u9FA5]{2,5})*";
    NSPredicate *nameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nameRegex];
    return ![nameTest evaluateWithObject:name];
}

/*昵称验证，昵称不能是纯数字*/
+ (BOOL)isValidateNumName:(NSString *)name
{
    NSString *nameRegex = @"^[0-9]*$";
    NSPredicate *nameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nameRegex];
    return ![nameTest evaluateWithObject:name];
}

/*昵称验证，昵称中不能含特殊字符*/
+ (BOOL)isValidateSpecialName:(NSString *)name
{
    NSString *nameRegex = @"^\\w*$";
    NSPredicate *nameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nameRegex];
    return [nameTest evaluateWithObject:name];
}

/*密码验证，密码为8-16位数字加字母*/
+ (BOOL)isValidatePwd:(NSString *)pwd
{
    NSString *pwdRegex = @"^[a-zA-Z0-9]{8,16}$";
    NSPredicate *pwdTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pwdRegex];
    return [pwdTest evaluateWithObject:pwd];
}

/*固话 ＋ 手机号码验证*/
//+ (BOOL)isValidateMobile:(NSString *)mobile
//{
//    /*
//     //手机号以13， 15，18开头，八个 \d 数字字符
//     NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
//     NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
//     return [phoneTest evaluateWithObject:mobile];
//     */
//
//    // 固话 ＋ 手机号以13,14,15,17,18开头，八个 \d 数字字符
//    NSString *phoneRegex = @"^(0[0-9]{2,4})?([2-9][0-9]{6,8})+(/-[0-9]{1,4})?$|(^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\\d{8}$)";
//    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
//    //    NSLog(@"phoneTest is %@",phoneTest);]
//    return [phoneTest evaluateWithObject:mobile];
//}

+ (BOOL)isValidateMobile:(NSString *)mobile
{
    if (mobile.length>30||mobile.length<6||[self IsChinese:mobile]||[self isValidateEnglish:mobile]) {
        return NO;
    }
    return YES;
}

//手机号码是否正确

+ (BOOL)valiMobile:(NSString *)mobile {
    
    if (mobile.length != 11) {
        return NO;
    } else {
        /**
         * 移动号段正则表达式
         */
        NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(17[0-9])|(18[2-4,7-8]))\\d{8}$";

        /**
         * 联通号段正则表达式
         */
        NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(17[0-9])|(18[5,6]))\\d{8}$";

        /**
         * 电信号段正则表达式
         */
        NSString *CT_NUM = @"^((133)|(153)|(17[0-9])|(18[0,1,9]))\\d{8}$";

        NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];

        BOOL isMatch1 = [pred1 evaluateWithObject:mobile];

        NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];

        BOOL isMatch2 = [pred2 evaluateWithObject:mobile];

        NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];

        BOOL isMatch3 = [pred3 evaluateWithObject:mobile];

        
        if (isMatch1 || isMatch2 || isMatch3) {
            return YES;
        } else {
            return NO;
        }
    }
    return YES;
}

+ (BOOL)isValidateEnglish:(NSString *)str
{
    if([str rangeOfString:@"a"].location!=NSNotFound||
       [str rangeOfString:@"b"].location!=NSNotFound||
       [str rangeOfString:@"c"].location!=NSNotFound||
       [str rangeOfString:@"d"].location!=NSNotFound||
       [str rangeOfString:@"e"].location!=NSNotFound||
       [str rangeOfString:@"f"].location!=NSNotFound||
       [str rangeOfString:@"g"].location!=NSNotFound||
       [str rangeOfString:@"h"].location!=NSNotFound||
       [str rangeOfString:@"i"].location!=NSNotFound||
       [str rangeOfString:@"j"].location!=NSNotFound||
       [str rangeOfString:@"k"].location!=NSNotFound||
       [str rangeOfString:@"l"].location!=NSNotFound||
       [str rangeOfString:@"m"].location!=NSNotFound||
       [str rangeOfString:@"n"].location!=NSNotFound||
       [str rangeOfString:@"o"].location!=NSNotFound||
       [str rangeOfString:@"p"].location!=NSNotFound||
       [str rangeOfString:@"q"].location!=NSNotFound||
       [str rangeOfString:@"r"].location!=NSNotFound||
       [str rangeOfString:@"s"].location!=NSNotFound||
       [str rangeOfString:@"t"].location!=NSNotFound||
       [str rangeOfString:@"u"].location!=NSNotFound||
       [str rangeOfString:@"v"].location!=NSNotFound||
       [str rangeOfString:@"w"].location!=NSNotFound||
       [str rangeOfString:@"x"].location!=NSNotFound||
       [str rangeOfString:@"y"].location!=NSNotFound||
       [str rangeOfString:@"z"].location!=NSNotFound||
       [str rangeOfString:@"A"].location!=NSNotFound||
       [str rangeOfString:@"B"].location!=NSNotFound||
       [str rangeOfString:@"C"].location!=NSNotFound||
       [str rangeOfString:@"D"].location!=NSNotFound||
       [str rangeOfString:@"E"].location!=NSNotFound||
       [str rangeOfString:@"F"].location!=NSNotFound||
       [str rangeOfString:@"G"].location!=NSNotFound||
       [str rangeOfString:@"H"].location!=NSNotFound||
       [str rangeOfString:@"I"].location!=NSNotFound||
       [str rangeOfString:@"J"].location!=NSNotFound||
       [str rangeOfString:@"K"].location!=NSNotFound||
       [str rangeOfString:@"L"].location!=NSNotFound||
       [str rangeOfString:@"M"].location!=NSNotFound||
       [str rangeOfString:@"N"].location!=NSNotFound||
       [str rangeOfString:@"O"].location!=NSNotFound||
       [str rangeOfString:@"P"].location!=NSNotFound||
       [str rangeOfString:@"Q"].location!=NSNotFound||
       [str rangeOfString:@"R"].location!=NSNotFound||
       [str rangeOfString:@"S"].location!=NSNotFound||
       [str rangeOfString:@"T"].location!=NSNotFound||
       [str rangeOfString:@"U"].location!=NSNotFound||
       [str rangeOfString:@"V"].location!=NSNotFound||
       [str rangeOfString:@"W"].location!=NSNotFound||
       [str rangeOfString:@"X"].location!=NSNotFound||
       [str rangeOfString:@"Y"].location!=NSNotFound||
       [str rangeOfString:@"Z"].location!=NSNotFound||
       [str rangeOfString:@"a"].location!=NSNotFound||
       [str rangeOfString:@"a"].location!=NSNotFound) {
        return YES;
    } else {
        return NO;
    }
}

//判断是否有中文
+ (BOOL)IsChinese:(NSString *)str {
    for(int i=0; i< [str length];i++){
        int a = [str characterAtIndex:i];
        if( a > 0x4e00 && a < 0x9fff) {
            return YES;
        }
    }
    return NO;
}

/*车牌号验证*/
+ (BOOL) isValidateCarNo:(NSString *)carNo
{
    NSString *carRegex = @"^[A-Za-z]{1}[A-Za-z_0-9]{5}$";
    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
    NSLog(@"carTest is %@",carTest);
    return [carTest evaluateWithObject:carNo];
}

//get32为随机密码
+ (NSString*)get32Random{
    
    int value = arc4random() % 10;
    
    NSDateFormatter *tFormat=[[NSDateFormatter alloc] init];
    [tFormat setDateFormat:@"yyyyMMddHHmmss"];
    NSString *tResult=[NSString stringWithFormat:@"FHGJFI1DH2FUGJE56%@%d",[tFormat stringFromDate:[NSDate date]],value];
    return tResult;
}



+ (BOOL)isValidateIdentity:(NSString *)identityString {
    if (identityString.length != 18) return NO;
    // 正则表达式判断基本 身份证号是否满足格式
    NSString *regex2 = @"^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$";
    NSPredicate *identityStringPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    //如果通过该验证，说明身份证格式正确，但准确性还需计算
    if(![identityStringPredicate evaluateWithObject:identityString]) return NO;
    //** 开始进行校验 *//
    //将前17位加权因子保存在数组里
    NSArray *idCardWiArray = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"];
    //这是除以11后，可能产生的11位余数、验证码，也保存成数组
    NSArray *idCardYArray = @[@"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"];
    //用来保存前17位各自乖以加权因子后的总和
    NSInteger idCardWiSum = 0;
    for(int i = 0;i < 17;i++) {
    NSInteger subStrIndex  = [[identityString substringWithRange:NSMakeRange(i, 1)] integerValue];
    NSInteger idCardWiIndex = [[idCardWiArray objectAtIndex:i] integerValue];
    idCardWiSum      += subStrIndex * idCardWiIndex;
    }
    //计算出校验码所在数组的位置
    NSInteger idCardMod=idCardWiSum%11;
    //得到最后一位身份证号码
    NSString *idCardLast= [identityString substringWithRange:NSMakeRange(17, 1)];
    //如果等于2，则说明校验码是10，身份证号码最后一位应该是X
    if(idCardMod==2) {
    if(![idCardLast isEqualToString:@"X"]||[idCardLast isEqualToString:@"x"]) {
      return NO;
    }
    }else{
    //用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
    if(![idCardLast isEqualToString: [idCardYArray objectAtIndex:idCardMod]]) {
      return NO;
    }
    }
    return YES;
}

@end
